package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.model.MessageBean;

import static cn.acewill.yxd.yunxundian.R.id.isReadOrNot;


/**
 * Created by XXX on 2017/4/3.
 */
public class messageNoticeAdapter extends BaseAdapter{
    private Context context;
    private ArrayList<MessageBean> list;
    private LookCliclListener lookCliclListener;

    public messageNoticeAdapter(Context context, ArrayList<MessageBean> list, LookCliclListener lookCliclListener){
        this.context=context;
        this.list=list;
        this.lookCliclListener=lookCliclListener;
    }
    @Override
    public int getCount() {
        return list==null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.message_warm,parent,false);
            holder=new ViewHolder();
            holder.msgTitle= (TextView) convertView.findViewById(R.id.msgTitle);
            holder.msgContent= (TextView) convertView.findViewById(R.id.msgContent);
            holder.lookAll= (TextView) convertView.findViewById(R.id.lookAll);
            holder.isReadOrNot= (ImageView) convertView.findViewById(isReadOrNot);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        MessageBean bean=list.get(position);
        holder.msgTitle.setText(bean.getTitle());
        holder.msgContent.setText(bean.getMsg().replaceAll("<br/>","\n"));
        holder.lookAll.setText("查看全部");
        holder.lookAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                lookCliclListener.lookOnclick(position);
            }
        });
        if (bean.getIsread()==0){
            holder.isReadOrNot.setVisibility(View.VISIBLE);
        }else{
            holder.isReadOrNot.setVisibility(View.GONE);
        }

        return convertView;
    }

    class ViewHolder{
        TextView msgTitle;
        TextView msgContent;
        TextView lookAll;
        ImageView isReadOrNot;
    }

    public interface LookCliclListener{
        void lookOnclick(int position);
    }
}
